home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / primitives / files.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  2KB  |  62 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from contextlib import contextmanager
  6. import logging
  7. import os
  8. import sys
  9. import time
  10. log = logging.getLogger('util.primitives.files')
  11.  
  12. def filecontents(filename):
  13.     
  14.     try:
  15.         f = _[2]
  16.         return f.read()
  17.     finally:
  18.         pass
  19.  
  20.  
  21.  
  22. def atomic_write(filename):
  23.     (path, filepart) = os.path.split(filename)
  24.     tempfile = os.path.join(path, '.%s.tmp' % filepart)
  25.     f = open(tempfile, 'w')
  26.     
  27.     try:
  28.         yield f
  29.     except Exception:
  30.         f.close()
  31.         os.remove(tempfile)
  32.         raise 
  33.     finally:
  34.         f.close()
  35.  
  36.     replace_file(filename, tempfile)
  37.  
  38. atomic_write = contextmanager(atomic_write)
  39. if sys.platform == 'win32':
  40.     from ctypes import windll, WinError
  41.     ReplaceFileW = windll.kernel32.ReplaceFileW
  42.     REPLACEFILE_WRITE_THROUGH = 1
  43.     
  44.     def replace_file(filename, replacement):
  45.         if not ReplaceFileW(filename, replacement, None, REPLACEFILE_WRITE_THROUGH, 0, 0):
  46.             raise WinError()
  47.         
  48.  
  49. else:
  50.     
  51.     def replace_file(filename, replacement):
  52.         backup = filename + '.backup.' + str(time.time())
  53.         os.rename(filename, backup)
  54.         
  55.         try:
  56.             os.rename(replacement, filename)
  57.         except:
  58.             os.rename(backup, filename)
  59.             raise 
  60.  
  61.  
  62.